package com.cmoine.ei18n;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.lang3.mutable.MutableObject;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock.Key;
import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesConfigurationBlock;
import org.eclipse.jdt.internal.ui.preferences.ProblemSeveritiesPreferencePage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.cmoine.ei18n"; //$NON-NLS-1$

	// The shared instance
	private static Activator plugin;

    private static class MLSImageRegistry extends ImageRegistry {
        private final ImageRegistry delegate;
        private final Bundle bundle;

        public MLSImageRegistry(Bundle bundle, ImageRegistry delegate) {
            this.bundle=bundle;
            this.delegate=delegate;
        }

        @Override
        public void dispose() {
            delegate.dispose();
        }

        @Override
        public boolean equals(Object obj) {
            return delegate.equals(obj);
        }

        @Override
        public Image get(String key) {
            Image result=delegate.get(key);
            if (result == null) {
                ImageDescriptor descriptor=ImageDescriptor.createFromURL(Activator.getURL(bundle, key));
                put(key, descriptor);
                result=delegate.get(key);
            }
            return result;
        }

        @Override
        public ImageDescriptor getDescriptor(String key) {
            ImageDescriptor result=delegate.getDescriptor(key);
            if (result == null) {
                ImageDescriptor descriptor=ImageDescriptor.createFromURL(Activator.getURL(bundle, key));
                put(key, descriptor);
                result=delegate.getDescriptor(key);
            }
            return result;
        }

        @Override
        public int hashCode() {
            return delegate.hashCode();
        }

        @Override
        public void put(String key, Image image) {
            delegate.put(key, image);
        }

        @Override
        public void put(String key, ImageDescriptor descriptor) {
            delegate.put(key, descriptor);
        }

        @Override
        public void remove(String key) {
            delegate.remove(key);
        }

        @Override
        public String toString() {
            return delegate.toString();
        }
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
        checkParameters();
		plugin = this;
	}

    private void checkParameters() {
        final MutableObject<Key> myKey=new MutableObject<Key>();
        for (Key key : ProblemSeveritiesConfigurationBlock.getKeys()) {
            if (key.getQualifier().equals(JavaCore.COMPILER_PB_NON_NLS_STRING_LITERAL)) {
                myKey.setValue(key);
            }
        }
        Object object=JavaCore.getOptions().get(JavaCore.COMPILER_PB_NON_NLS_STRING_LITERAL);
        if ("ignore".equals(object)) { //$NON-NLS-1$
            Display.getDefault().asyncExec(new Runnable() {
                @Override
                public void run() {
                    Shell shell=PlatformUI.getWorkbench().getModalDialogShellProvider().getShell();
                    MessageDialog.openWarning(shell, "Warning", "You must enable a JDT option ('"
                            + (myKey.getValue() == null ? "???" : myKey.getValue().getName()) + "'), whould you like to set it automatically ?");
                    System.out.println("Activator.checkParameters().new Runnable() {...}.run()");
                    JavaCore.getOptions().put(JavaCore.COMPILER_PB_NON_NLS_STRING_LITERAL, "warning");
                    PreferencesUtil.createPreferenceDialogOn(shell, ProblemSeveritiesPreferencePage.PREF_ID, null, null).open();
                }
            });
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

    public IDialogSettings getOrCreateDialogSettings(Class<?> clazz) {
        String sectionName=clazz.getName();
        IDialogSettings section=getDialogSettings().getSection(sectionName);
        if (section == null)
            section=getDialogSettings().addNewSection(sectionName);

        return section;
    }

	public static void log(int severity, String message, Throwable e) {
		getDefault().getLog().log(new Status(severity, PLUGIN_ID, message, e));
	}

    @Override
    protected ImageRegistry createImageRegistry() {
        ImageRegistry result=super.createImageRegistry();
        return new MLSImageRegistry(getBundle(), result);
    }

    public static URL getURL(String path) {
        if (getDefault() == null)
            return null;

        return getURL(getDefault().getBundle(), path);
    }

    public static URL getURL(Bundle bundle, String path) {
        if (bundle == null || !isReady(bundle.getState()))
            return null;

        // look for the image (this will check both the plugin and fragment
        // folders
        URL fullPathString=FileLocator.find(bundle, new Path(path), null);
        if (fullPathString == null) {
            try {
                fullPathString=new URL(path);
            } catch (MalformedURLException e) {
                return null;
            }
        }

        return fullPathString;
    }

    private static boolean isReady(int bundleState) {
        return (bundleState & (Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING)) != 0;
    }
}
